<script lang="ts" setup>
import { useRoute } from 'vue-router';
import { useHead } from '@vueuse/head';
import type { HeadObject } from '@vueuse/head';

import BaseLayout from './base.layout.vue';
import FavoriteButton from '@/components/FavoriteButton.vue';
import type { Tool } from '@/tools/tools.types';

import { Swiper, SwiperSlide } from 'swiper/vue';
import { Autoplay } from 'swiper/modules';
import 'swiper/css';
const modules = [Autoplay];

const route = useRoute();

const head = computed<HeadObject>(() => ({
  title: `${route.meta.name} - IT Tools`,
  meta: [
    {
      name: 'description',
      content: route.meta?.description as string,
    },
    {
      name: 'keywords',
      content: ((route.meta.keywords ?? []) as string[]).join(','),
    },
  ],
}));
useHead(head);
const { t } = useI18n();

// const i18nKey = computed<string>(() => route.path.trim().replace('/', ''));
// const toolTitle = computed<string>(() => t(`tools.${i18nKey.value}.title`, String(route.meta.name)));
// const toolDescription = computed<string>(() => t(`tools.${i18nKey.value}.description`, String(route.meta.description)));

const toolTitle = computed<string>(() => {
  return t(`home.menu.${String(route.meta.name)}.title`, String(route.meta.name));
});
const toolDescription = computed<string>(() => {
  return t(`home.menu.${String(route.meta.name)}.description`, String(route.meta.description));
});

let adFlag = false;
function closeAd() {
  adFlag = false;
}

const adList = [
  {
    title: '【腾讯云】腾讯云图，像PPT一样简单的数据可视化工具。5元搞定数据可视化，模板丰富，拖拖拽拽就能做出好看的可视化大屏。',
    img: '/tcloud-data-ad1040x100.png',
    href: 'https://curl.qcloud.com/cPVkAt8b',
    text: '',
  },
];
</script>

<template>
  <BaseLayout>
    <div class="tool-layout">
      <div class="tool-header">
        <div flex flex-nowrap items-center justify-between>
          <n-h1>
            {{ toolTitle }}
          </n-h1>

          <div>
            <FavoriteButton :tool="{ name: route.meta.name } as Tool" />
          </div>
        </div>

        <div class="separator" />

        <div class="description">
          {{ toolDescription }}
        </div>
      </div>
    </div>

    <div class="tool-content">
      <slot />
    </div>
    <div class="ad-layout" style="display: none" v-show="adFlag">
      <a href="#" class="close" @click="closeAd()"> </a>

        <div class="ad-separator" />

        <!-- <a href="https://3.cn/1Pg-lFAM" title="京东云·云惠购">
          <div class="ad-img-box">
            <img class="ad-img" alt="京东云·云惠购" src="/jcloud-ad.jpg" />
            <span></span>
          </div>
        </a> -->
        <div class="swiperBox">
        <swiper
          direction="horizontal"
          :loop="true"
          :autoplay="{ disableOnIntercation: false, autoplay: false, delay: 2400 }"
          slidesPerView="auto"
          :spaceBetween="0"
          :centeredSlides="true"
          :modules="modules"
        >
          <swiper-slide v-for="(item, key) in adList" :key="key">
            <van-image w-full h-full :src="item.url" fit="cover" />
            <!-- alert(item.title); -->
            <a :href=item.href :title=item.title>
              <div class="ad-img-box">
                <img class="ad-img" :alt=item.title :src=item.img />
                <span>{{ item.text }}</span>
              </div>
            </a>
          </swiper-slide>
        </swiper>
      </div>

      <!-- </div> -->
    </div>
  </BaseLayout>
</template>

<style lang="less" scoped>
.tool-content {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 16px;

  ::v-deep(& > *) {
    flex: 0 1 600px;
  }
}

.tool-layout {
  max-width: 600px;
  margin: 0 auto;
  box-sizing: border-box;

  .tool-header {
    padding: 40px 0;
    width: 60%;

    .n-h1 {
      opacity: 0.9;
      font-size: 40px;
      font-weight: 400;
      margin: 0;
      line-height: 1;
    }

    .separator {
      width: 200px;
      height: 2px;
      opacity: 0.2;

      margin: 10px 0;
    }

    .description {
      margin: 0;
      opacity: 0.7;
    }
  }
}

.ad-layout {
  position: absolute;
  flex-direction: row;
  justify-content: center;
  align-items: flex-start;
  max-width: 100%;
  margin: 0 auto;
  bottom: 0;
  left: 0;
  // margin: 10px 0
  // box-sizing: border-box;
  // opacity: 0.2;
  // background: rgb(131, 120, 120);
  .ad-separator {
      // width: 200px;
      height: 10px;
      opacity: 1;
      margin: 10px 0;
    }
  .ad-img-box {
    position: relative;

    display: flex;
    // margin: 0 0;
    margin: 0 auto;
    width: 100%;
    height: 100px;
    // background: rgb(20, 20, 20);
    background-size:contain;


    .ad-img {
      display: flex;
      position: relative;
      width: 100%;
      opacity: 1;/*设置透明度*/
      z-index: 1;
      // background-size: cover;
      background-size:contain;
      // margin: 0 auto;
      // width: 100%;

      // background-image: url("/jcloud-ad.jpg");
      // background-size: 1920px 70px;
      // span {
      //         font-style: oblique;
      // }
      // background-repeat: no-repeat;
      object-fit: cover;

    }
    span {
      position: absolute;/*设为绝对定位*/
      width: inherit;
      text-align: center;
      font-size: 60px;
      font-weight: bolder;
      color: #fdf9f9;
      text-align: center;
      font-family: "楷体", "宋体",serif;
      font-style: italic;
      z-index: 2;/*置于上层*/
      display: inline;
    }
  }
}

.close {
  opacity: 0.3;
}
.close:hover {
  opacity: 1;
}
.close:before, .close:after {
  float: right;
  position: relative;
  right: 20px;
  top: 5px;
  content: ' ';
  height: 20px;
  width: 1px;
  background-color: #333;
}
.close:before {
  transform: rotate(45deg);
}
.close:after {
  transform: rotate(-45deg);
}

.container {
  width: 500px;
  height: 200px;
  margin:30px 20px;
  background-color: #f1f1f1;
}
</style>
